#include "dialog.h"

Dialog::Dialog(QWidget *parent)
    : QDialog(parent)
{
    setWindowTitle(tr("对话框实例"));
    this->resize(400, 400);
    QGridLayout *mainLayout = new QGridLayout(this);

    // 标准文件对话框控件、槽函数
    fileButton = new QPushButton(tr("标准文件对话框"));
    fileEdit = new QLineEdit;
    mainLayout->addWidget(fileButton, 0, 0);
    mainLayout->addWidget(fileEdit, 0, 1);
    connect(fileButton, &QPushButton::clicked, this, &Dialog::showFileDialog);

    // 标准颜色对话框控件、槽函数
    colorButton = new QPushButton(tr("标准颜色对话框"));
    // QFrame用于根据用户选择的不同的颜色更新的不同的背景
    colorFrame = new QFrame;
    colorFrame->setFrameShape(QFrame::Box);
    colorFrame->setAutoFillBackground(true);
    mainLayout->addWidget(colorButton, 1, 0);
    mainLayout->addWidget(colorFrame, 1, 1);
    connect(colorButton, &QPushButton::clicked, this, &Dialog::showColorDialog);

    // 标准字体对话框控件、槽函数
    fontButton = new QPushButton(tr("标准字体对话框"));
    fontEdit = new QLineEdit;
    fontEdit->setText(tr("Hello shige"));
    mainLayout->addWidget(fontButton, 2, 0);
    mainLayout->addWidget(fontEdit, 2, 1);
    connect(fontButton, &QPushButton::clicked, this, &Dialog::showFontDialog);

    // 标准输入框控件、槽函数
    inputButton = new QPushButton(tr("标准输入对话框"));
    mainLayout->addWidget(inputButton, 3, 0);
    connect(inputButton, &QPushButton::clicked, this, &Dialog::showInputDialog);

    // 消息框控件、槽函数
    msgButton = new QPushButton(tr("标准消息对话框"));
    mainLayout->addWidget(msgButton, 3, 1);
    connect(msgButton, &QPushButton::clicked, this, &Dialog::showMsgDlg);
}

Dialog::~Dialog()
{

}

void Dialog::showFileDialog()
{
    // 获取打开的文件对话框中选中的文件的名称，getOpenFileName()函数
    QString s = QFileDialog::getOpenFileName(this, "open file dialog", "/");

    // 将获取到的名称添加到编辑框中
    fileEdit->setText(s);
}

void Dialog::showColorDialog()
{
    // 调用颜色对话框选择颜色,getColor()函数
    QColor color = QColorDialog::getColor(Qt::red);

    // 将在调色板选择的颜色放到背景板中
    if (color.isValid())
    {
        colorFrame->setPalette((QPalette)color);
    }
}

void Dialog::showFontDialog()
{
    bool ok;
    // 调用getFont()函数，如果用户点击ok就改变字体
    QFont f = QFontDialog::getFont(&ok);

    if (ok)
    {
        fontEdit->setFont(f);
    }
}

void Dialog::showInputDialog()
{
    inputDlg = new InputDlg(this);
    inputDlg->show();
}

void Dialog::showMsgDlg()
{
    msgDlg = new MsgBoxDlg(this);
    msgDlg->show();
}

